var imgCheck = true;
var receiptCheck = true;
/**
 * ajax请求
 * 
 * @param {}
 *            URL
 * @param {}
 *            param
 * @param {}
 *            method 方法
 * @param {}
 *            syn 是否同步 [false 同步，true - 异步]
 * @param {}
 *            callback
 */
function ajaxReq(URL, param, method, syn, callback) {
	jQuery.ajax({
		url : URL,
		// processData: false,
		type : method,
		async : syn,
		data : param,
		success : callback

	});
}

/**
 * 根据字符[sign],把字符串内容[content]解析为数组形式
 * 
 * 参数：content 被分割的字符串 参数：sign 分割符号
 * 
 * 例如：content为[01,02,03] , 解析后返回{[01],[02],[03]}数组
 */
function mipssToArray(content, sign) {
	return content.split(sign);
}
/**
 * js日期转换
 */
function getDate(strDate) {
	var date = eval('new Date('
			+ strDate.replace(/\d+(?=-[^-]+$)/, function(a) {
				return parseInt(a, 10) - 1;
			}).match(/\d+/g) + ')');
	return date;
}
function compareDate(date1, date2) {
	if (date1.getTime() > date2.getTime()) {
		return true;
	} else {
		return false;
	}
}

/**
 * 只输入数字
 */
function keyNumPress(){
	var keyCode = event.keyCode;
	if(keyCode>=48 && keyCode<=57){
		event.returnValue = true;
	}else{
		event.returnValue = false;
	}
}

/**
 * 只输入整数
 */
function keyNumPress(val){
	var keyCode = event.keyCode;
	if(keyCode>=48 && keyCode<=57){
		var reg = /[^0-9]/g;
    	if(!reg.test(val)){
    		event.returnValue = false;
    	}
		event.returnValue = true;
	}else{
		event.returnValue = false;
	}
}

String.prototype.trim = function(){return this.replace(/^\s+|\s+$/g,"");}
//身份证校验
//S2023103127804-粤金办发〔2023〕27号-外国人永久居留身份证规则调整系统需求 update by huanghk 20231115 start
var vcity={ 11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",
        21:"辽宁",22:"吉林",23:"黑龙江",31:"上海",32:"江苏",
        33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",
        42:"湖北",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",
        51:"四川",52:"贵州",53:"云南",54:"西藏",61:"陕西",62:"甘肃",
        63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门"//,91:"国外"
       };
//S2023103127804-粤金办发〔2023〕27号-外国人永久居留身份证规则调整系统需求 update by huanghk 20231115 end
checkCard = function(card){
	if(isCardNo(card) === false){//校验长度，类型
		return "身份证号码不正确或非18位";
	}

	if(checkProvince(card) === false){//检查省份
	    return "身份证号码不正确";
	}

	if(checkBirthday(card) === false){//校验生日
	    return '身份证号码生日不正确';
	}

	if(checkParity(card) === false){//检验位的检测
	    return "您的身份证校验位不正确,请重新输入";
	}
	return "";
};


	//检查号码是否符合规范，包括长度，类型
isCardNo = function(card){
	//身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X
	//var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)/;
	var reg = /(^\d{17}(\d|[Xx])$)/;
	if(reg.test(card) === false){
		return false;
	}
	return true;
};


checkProvince = function(card){//取身份证前两位,校验省份
	var province = card.substr(0,2);
	if(vcity[province] == undefined){
		return false;
	}
	return true;
};

checkBirthday = function(card){//检查生日是否正确
	var len = card.length;
	if(len == '15'){//身份证15位时，次序为省（3位）市（3位）年（2位）月（2位）日（2位）校验位（3位），皆为数字
	    var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/; 
	    var arr_data = card.match(re_fifteen);
	    var year = arr_data[2];
	    var month = arr_data[3];
	    var day = arr_data[4];
	    var birthday = new Date('19'+year+'/'+month+'/'+day);
	    return verifyBirthday('19'+year,month,day,birthday);
	}

	if(len == '18'){//身份证18位时，次序为省（3位）市（3位）年（4位）月（2位）日（2位）校验位（4位），校验位末尾可能为X
	    var re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|[Xx])$/;
	    var arr_data = card.match(re_eighteen);
	    var year = arr_data[2];
	    var month = arr_data[3];
	    var day = arr_data[4];
	    var birthday = new Date(year+'/'+month+'/'+day);
	    return verifyBirthday(year,month,day,birthday);
	}
	return false;
};


verifyBirthday = function(year,month,day,birthday){//校验日期
	var now = new Date();
	var now_year = now.getFullYear();

	if(birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day){//年月日是否合理
		
		/*var time = now_year - year;
		if(time >= 3 && time <= 100){//判断年份的范围（3岁到100岁之间)
			return true;
		}
		return false;
		*/
		return true;
	}
	return false;
};


checkParity = function(card){//校验位的检测
//15位转18位
//	card = changeFivteenToEighteen(card);
	var len = card.length;
	if(len == '18'){
	    var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
	    var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 
	    var cardTemp = 0, i, valnum; 
	    for(i = 0; i < 17; i ++){
	        cardTemp += parseInt(card.substr(i, 1)) * arrInt[i];
	    }
	    valnum = arrCh[cardTemp % 11];
	    if (valnum == card.substr(17, 1).toUpperCase()){
	        return true;
	    }
	    return false;
	}
	return false;
};

//15位转18位身份证号
changeFivteenToEighteen = function(card){
	if(card.length == '15'){
	    var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
	    var arrCh = new Array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'); 
	    var cardTemp = 0, i;   
	    card = card.substr(0, 6) + '19' + card.substr(6, card.length - 6);
	    for(i = 0; i < 17; i ++) 
	    { 
	        cardTemp += card.substr(i, 1) * arrInt[i]; 
	    } 
	    card += arrCh[cardTemp % 11]; 
	    return card;
	}
	return card;
};// JavaScript Document


/**
 * 格式化数字显示方式 
 * 用法
 * formatNumber(12345.999,'#,##0.00');
 * formatNumber(12345.999,'#,##0.##');
 * formatNumber(123,'000000');
 * @param num
 * @param pattern
 */
function formatNumber(num,pattern){
  var strarr = num?num.toString().split('.'):['0'];
  var fmtarr = pattern?pattern.split('.'):[''];
  var retstr='';

  // 整数部分
  var str = strarr[0];
  var fmt = fmtarr[0];
  var i = str.length-1;  
  var comma = false;
  for(var f=fmt.length-1;f>=0;f--){
    switch(fmt.substr(f,1)){
      case '#':
        if(i>=0 ) retstr = str.substr(i--,1) + retstr;
        break;
      case '0':
        if(i>=0) retstr = str.substr(i--,1) + retstr;
        else retstr = '0' + retstr;
        break;
      case ',':
        comma = true;
        retstr=','+retstr;
        break;
    }
  }
  if(i>=0){
    if(comma){
      var l = str.length;
      for(;i>=0;i--){
        retstr = str.substr(i,1) + retstr;
        if(i>0 && ((l-i)%3)==0) retstr = ',' + retstr; 
      }
    }
    else retstr = str.substr(0,i+1) + retstr;
  }

  retstr = retstr+'.';
  // 处理小数部分
  str=strarr.length>1?strarr[1]:'';
  fmt=fmtarr.length>1?fmtarr[1]:'';
  i=0;
  for(var f=0;f<fmt.length;f++){
    switch(fmt.substr(f,1)){
      case '#':
        if(i<str.length) retstr+=str.substr(i++,1);
        break;
      case '0':
        if(i<str.length) retstr+= str.substr(i++,1);
        else retstr+='0';
        break;
    }
  }
  return retstr.replace(/^,+/,'').replace(/\.$/,'');
}

/**
 * 根据身份证号设置生日性别（18位）
 * @param {} idNo
 * @param {} birthday
 * @param {} gender
 */
function setBirthAndGenderFromIdNo(idNo,birthday,gender){
   		var id_birth = idNo.substring(6,14);
		/*截取年份*/
		var id_year = id_birth.substring(0,4);
		/*截取月份*/
		var id_month = id_birth.substring(4,6);
 		/*截取天数*/
 		var id_day = id_birth.substring(6,8);
	 	var _birthday = id_year+"-"+id_month+"-"+id_day;
	 	if(birthday){
	 	 	jQuery("#"+birthday).val(_birthday);
	 	}
	 	var is_fandm = idNo.substring(16,17);
	 	if(gender){
	 	   if(is_fandm%2==1){
				jQuery("#"+gender).val("M");
				jQuery("#"+gender).selectmenu('refresh');
				//jQuery("#"+gender).refresh();
		 	}else if(is_fandm%2==0){
		 		jQuery("#"+gender).val("F");
		 		jQuery("#"+gender).selectmenu('refresh');
			}
	 	}
	 	
}


//placeholder
var doc = document,  
inputs = doc.getElementsByTagName('input'),  
   
// 检测 placeholder 支持  
supportPlaceholder = 'placeholder' in doc.createElement('input'),  
   
/* 
 * 创建 placeholder 
 * @param {ELEMENT} input 传入的 input 对象 
 * @return {VOID} 
 * @author: sofish Lin http://sofish.de 
 */  
placeholder = function (input) {  
    var text = input.getAttribute('placeholder'),  
        defaultValue = input.defaultValue;  
   
    // 制作一个假 placeholder  
    // 需要 css 配合，在 JS 中设置 style 并不太好  
    input.value = text;  
   
    // 聚焦去掉假 placeholder  
    input.onfocus = function () {  
        if (input.value === defaultValue || input.value === text) {  
            this.value = '';  
        }  
    }  
   
    // 当失焦值为空时，补回 placeholder 的值  
    input.onblur = function () {  
        if (input.value === '') {  
            this.value = text;  
        }  
    }  
};  
   
if (!supportPlaceholder) {  
    for (var i = 0, len = inputs.length; i < len; i++) {  
        var input = inputs[i],  
            text = input.getAttribute('placeholder');  
   
        // 当 input[type=text] 并且 placeholder 值不为空是执行  
        if (input.type === 'text' && text) {  
            placeholder(input);  
        }  
    }  
}  
